package com.wubotao.activity;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;

public class PullToRefreshActivity extends ListActivity {
	private LinkedList<User> mListItems;
	PullToRefreshListView listView;
	private LinearLayout mLoadView;
	private LayoutInflater mInflater;
	private ProgressBar mLoadViewProgress;
	private TextView mLoadViewText;
	UserAdapter adapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pull_to_refresh);

		listView = (PullToRefreshListView) getListView();
		mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		mLoadView = (LinearLayout) mInflater.inflate(
				R.layout.pull_to_load_footer, null);
		mLoadViewProgress = (ProgressBar) mLoadView
				.findViewById(R.id.pull_to_refresh_progress);
		mLoadViewText = (TextView) mLoadView
				.findViewById(R.id.pull_to_refresh_text);

		// Set a listener to be invoked when the list should be refreshed.
		listView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				// Do work to refresh the list here.
				new HeaderTask().execute();
			}
		});

		listView.addFooterView(mLoadView);
		mLoadView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View paramView) {
				new FooterTask().execute();
			}
		});

		mListItems = new LinkedList<User>();
		User u1 = new User("Jack", "CA");
		User u2 = new User("Mike", "WA");
		User u3 = new User("Kate", "NJ");
		User u4 = new User("Jack", "NJ");
		User u5 = new User("Mike", "NJ");
		User u6 = new User("Kate", "NJ");
		User u7 = new User("Jack", "NJ");
		User u8 = new User("Kate", "NJ");
		User u9 = new User("Mike", "NJ");
		mListItems.add(u1);
		mListItems.add(u2);
		mListItems.add(u3);
		mListItems.add(u4);
		mListItems.add(u5);
		mListItems.add(u6);
		mListItems.add(u7);
		mListItems.add(u8);
		mListItems.add(u9);

		adapter = new UserAdapter(this, mListItems);

		setListAdapter(adapter);
	}

	private class HeaderTask extends AsyncTask<Void, Void, List<User>> {

		@Override
		protected List<User> doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				;
			}
			return null;
		}

		@Override
		protected void onPostExecute(List<User> result) {
			User user = new User("Test", "NY");
			mListItems.addFirst(user);

			// Call onRefreshComplete when the list has been refreshed.
			((PullToRefreshListView) getListView()).onRefreshComplete();

			super.onPostExecute(result);
		}
	}

	private class FooterTask extends AsyncTask<Void, Void, String[]> {

		@Override
		protected void onPreExecute() {
			mLoadViewProgress.setVisibility(View.VISIBLE);
			mLoadViewText.setText(R.string.pull_to_refresh_refreshing_label);
			super.onPreExecute();
		}

		@Override
		protected String[] doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				;
			}
			return null;
		}

		@Override
		protected void onPostExecute(String[] result) {
			User user = new User("Test", "AL");
			mListItems.add(user);
			adapter.notifyDataSetChanged();

			mLoadViewProgress.setVisibility(View.INVISIBLE);
			mLoadViewText.setText(R.string.pull_to_refresh_tap_label);

			super.onPostExecute(result);
		}
	}

	class UserAdapter extends BaseAdapter {

		private List<User> list;
		private Context context;
		LayoutInflater inflater;

		public UserAdapter(Context context, List<User> list) {
			super();
			this.list = list;
			this.context = context;
			inflater = LayoutInflater.from(this.context);
		}

		@Override
		public int getCount() {
			return list == null ? 0 : list.size();
		}

		@Override
		public Object getItem(int location) {
			return list.get(location);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = null;
			if (convertView == null) {
				view = inflater.inflate(R.layout.list_item, parent, false);
				ViewHolder holder = new ViewHolder();
				holder.name = (TextView) view.findViewById(R.id.tv_name);
				holder.address = (TextView) view.findViewById(R.id.tv_address);

				view.setTag(holder);
			} else {
				view = convertView;
			}

			ViewHolder holder = (ViewHolder) view.getTag();
			User user = list.get(position);
			holder.name.setText(user.name);
			holder.address.setText(user.address);

			return view;
		}

	}

	class User implements Serializable {
		private static final long serialVersionUID = 6534804402384171802L;

		public String name;
		public String address;

		public User(String name, String address) {
			super();
			this.name = name;
			this.address = address;
		}

		public User() {
			super();
		}

	}

	class ViewHolder {
		public TextView name, address;
	}
}
